/*
 * Copyright (c) 2012 Johan Fylling
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy 
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights 
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
 * copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
 * THE SOFTWARE.
 */

part of tools;

class InputAction {
  String method;
  int    index;
  
  InputAction (String this.method, int this.index);
}


class KeyboardAction extends InputAction {
  KeyboardAction (int index) : super(Keyboard.INPUT_TYPE, index);
}


class MouseAction extends InputAction {
  MouseAction (int index) : super(Mouse.INPUT_TYPE, index);
}


/**
 * Manager of [InputMethod]s.
 * Provides conveniency functions for registering a set of [InputAction]s on an ID.
 * Default InputMethods are registered for keyboard and mouse.
 */
class InputManager implements Service {
  final Map<String, InputMethod>       _inputMethods = new Map<String, InputMethod>();
  final Map<String, List<InputAction>> _actions = new Map<String, List<InputAction>>();
  final Keyboard                       _keyboard = new Keyboard();
  final Mouse                          _mouse;
  
  InputManager (Element element) : this._mouse = new Mouse(element) {
    addMethod(Keyboard.INPUT_TYPE, this._keyboard);
    addMethod(Mouse.INPUT_TYPE, this._mouse);
  }
  
  /** Updated all registered [InputMethod]s. */
  void update () {
    this._keyboard.update();
    this._mouse.update();
  }
  
  /** Returns the [InputMethod] corresponding to the provided [id], [null] if none is found. */
  InputMethod getMethod (String id) {
    return this._inputMethods[id];
  }
  
  /** Registers [method] on [id]. Any existing [InputMethod] on this id is overridden. */
  void addMethod (String id, InputMethod method) {
    this._inputMethods[id] = method;
  }
  
  /**
   * Registers [actions] on [actionId].
   * The actions are OR:ed; if any one of them is set when [isActionSet] is called, [true] is returned.
   * New actions will not replace older ones; instead, the new list is merged with the old.
   */
  void addAction (String actionId, List<InputAction> actions) {
    List<InputAction> oldActions = this._actions[actionId];
    if (oldActions == null) {
      oldActions = new List<InputAction>();
      this._actions[actionId] = oldActions;
    }
    oldActions.addAll(actions);
  }
  
  /**  Checks if any [InputAction] registered on [actionId] is set. */
  bool isActionSet (String actionId) {
    if (!this._mouse.mouseOver) {
      return false;
    }
    
    List<InputAction> actions = this._actions[actionId];
    if (actions == null) {
      return false;
    }
    
    for (InputAction input in actions) {
      InputMethod method = this._inputMethods[input.method];
      if (method != null && method.stateSet[input.index]) {
        return true;
      }
    }
    return false;
  }
}
